package com.immediatus.graphics.buffer;

import com.immediatus.graphics.texture.region.BaseTextureRegion;
import com.immediatus.util.FastFloatBuffer;

import static com.immediatus.graphics.vertex.RectangleVertexBuffer.VERTICES_PER_RECTANGLE;

public class TextureRegionBuffer extends BufferObject{

    protected final BaseTextureRegion _textureRegion;
    private boolean _flippedVertical;
    private boolean _flippedHorizontal;

    public TextureRegionBuffer(final BaseTextureRegion textureRegion_, final int drawType_, final boolean managed_){
        super(2 * VERTICES_PER_RECTANGLE, drawType_, managed_);
        this._textureRegion = textureRegion_;
    }

    public BaseTextureRegion getTextureRegion(){
        return this._textureRegion;
    }

    public boolean isFlippedHorizontal(){
        return this._flippedHorizontal;
    }

    public void setFlippedHorizontal(final boolean flippedHorizontal_){
        if (this._flippedHorizontal != flippedHorizontal_){
            this._flippedHorizontal = flippedHorizontal_;
            this.update();
        }
    }

    public boolean isFlippedVertical(){
        return this._flippedVertical;
    }

    public void setFlippedVertical(final boolean flippedVertical_){
        if (this._flippedVertical != flippedVertical_){
            this._flippedVertical = flippedVertical_;
            this.update();
        }
    }

    public synchronized void update(){
        final BaseTextureRegion textureRegion = this._textureRegion;

        final int x1 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateX1());
        final int y1 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateY1());
        final int x2 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateX2());
        final int y2 = Float.floatToRawIntBits(textureRegion.getTextureCoordinateY2());

        final int[] bufferData = this._BufferData;

        if (this._flippedVertical){
            if (this._flippedHorizontal){
                bufferData[0] = x2;
                bufferData[1] = y2;

                bufferData[2] = x2;
                bufferData[3] = y1;

                bufferData[4] = x1;
                bufferData[5] = y2;

                bufferData[6] = x1;
                bufferData[7] = y1;
            } else{
                bufferData[0] = x1;
                bufferData[1] = y2;

                bufferData[2] = x1;
                bufferData[3] = y1;

                bufferData[4] = x2;
                bufferData[5] = y2;

                bufferData[6] = x2;
                bufferData[7] = y1;
            }
        } else{
            if (this._flippedHorizontal){
                bufferData[0] = x2;
                bufferData[1] = y1;

                bufferData[2] = x2;
                bufferData[3] = y2;

                bufferData[4] = x1;
                bufferData[5] = y1;

                bufferData[6] = x1;
                bufferData[7] = y2;
            } else{
                bufferData[0] = x1;
                bufferData[1] = y1;

                bufferData[2] = x1;
                bufferData[3] = y2;

                bufferData[4] = x2;
                bufferData[5] = y1;

                bufferData[6] = x2;
                bufferData[7] = y2;
            }
        }

        final FastFloatBuffer buffer = this._FloatBuffer;
        buffer.position(0);
        buffer.put(bufferData);
        buffer.position(0);

        super.setHardwareBufferNeedsUpdate();
    }
}
